package aom.example.security.mapper;

import aom.example.security.domain.SysRoleEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:41
 * @since JDK 1.8
 */
@Mapper
public interface SysRoleMapper extends BaseMapper<SysRoleEntity> {

    /**
     * 查询用户的权限信息
     * @param uid
     * @return
     */
    @Select("select id, user_id,a.role_id,role_name,role_no,createDate from sys_user_role a LEFT JOIN sys_role b ON a.role_id=b.role_id where user_id=#{uid}")
    List<Map<String, Object>> selectRoleUserByUserId(@Param("uid")String uid);

    /**
     * 给用户添加权限
     * @param userId
     * @param roleId
     * @return
     */
    @Insert("INSERT into sys_user_role (user_id,role_id) VALUES (#{userid},#{roleid})")
    boolean insertSysUserRole(@Param("userid")String userId,@Param("roleid")String roleId);


    /**
     * 查询用户不具有的权限
     * @param uid
     * @return
     */
    @Select("select * from sys_role where role_id NOT in( " +
            " select role_id from (" +
            " select user_id,a.role_id,role_name,role_no,createDate from sys_user_role a LEFT JOIN sys_role b ON a.role_id=b.role_id where user_id=#{uid} " +
            ") a )")
    List<Map<String, Object>> selectUserNotRole(@Param("uid")String uid);


    /**
     * 删除用户与角色的关联
     * @param id
     * @return
     */
    @Delete("delete from sys_user_role where id=#{id}")
    boolean deleteSysUserRoleById(@Param("id") String id);

}
